import router from '../../router/index.js'
import store from '@/store'
import bus from '/static/js/bus.js'
import {Notification} from 'element-ui'
var ws
function open(){
	console.log(store.getters.api.webSocket_serve+store.getters.userInfo.id);
	ws = new WebSocket(store.getters.api.webSocket_serve+store.getters.userInfo.id);
	ws.onopen = function(evt) {
	  console.log("webSocket连接已打开"); 
	  // ws.send("Hello WebSockets!");
	};
	// 监听到消息
	ws.onmessage = function(evt) {
		console.log('roter',router.history.current.path);
		if(router.history.current.path!='/chat'){
			Notification({
			  title: '新消息',
			  message: '您有一条新消息在聊天界面查看',
			  type: 'success'
			});
		}
		
		let res=JSON.parse(evt.data)
		res.content=JSON.parse(decodeURI(res.content))
		bus.$emit('Chat',res)
		bus.$emit('ChatContainer',res)
	  // ws.close();
	};
	
	ws.onclose = function(evt) {
	  console.log("Connection closed.");
	};   
}

export default {
	open,
}